<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" label-width="80px">
      <!-- <el-form-item label="编号" prop="departmentNumber">
        <el-input v-model="form.departmentNumber" placeholder="请输入编号" />
      </el-form-item> -->
      <el-form-item label="系部名称" prop="departmentName">
        <span>{{ form.departmentName }}</span>
        <!-- <el-input v-model="form.departmentName" placeholder="请输入系部名称" /> -->
      </el-form-item>
      <el-form-item label="英文名称" prop="englishName">
        <el-input v-model="form.englishName" placeholder="请输入英文名称" />
      </el-form-item>
      <!-- <el-form-item label="系部代码" prop="code">
        <el-input v-model="form.code" placeholder="请输入系部代码" />
      </el-form-item> -->
      <el-form-item label="学院" prop="collegeId">
        <college-selector v-model="form.collegeId"></college-selector>
      </el-form-item>
      <el-form-item label="负责人" prop="userId">
        <el-input v-model="form.userId" placeholder="请输入负责人" />
      </el-form-item>
      <el-form-item label="开办时间" prop="openingTime">
        <el-date-picker
          clearable
          v-model="form.openingTime"
          type="date"
          value-format="timestamp"
          placeholder="选择开办时间"
        />
      </el-form-item>
      <!-- <el-form-item label="机构编号" prop="orgId">
        <el-input v-model="form.orgId" placeholder="请输入机构编号" />
      </el-form-item> -->
      <!-- <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注" />
      </el-form-item> -->
      <el-form-item label="系部简介">
        <editor v-model="form.remark" :min-height="192" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  createDepartments,
  updateDepartments,
} from "@/api/collegebase/departments";
import common from "@/api/collegebase/common";
import Editor from '@/components/Editor';
import { DICT_TYPE, getDictDatas } from "@/utils/dict";
import CollegeSelector from "@/components/CollegeSelector";
export default {
  name: "DepartmentsEdit",
  components: { CollegeSelector, Editor },
  data() {
    return {
      editor: null,
      html: '<p>hello</p>',
      toolbarConfig: {},
      editorConfig: { placeholder: '请输入内容...' },
      mode: 'default', // or 'simple'
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        departmentName: [
          { required: true, message: '请输入系部名称', trigger: 'blur' }
        ],
        collegeId: [
          { required: true, message: '请选择学院', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '请输入负责人', trigger: 'blur' }
        ],
        openingTime: [
          { required: true, message: '请选择开办时间', trigger: 'blur' }
        ],

      },
      typeDictData: getDictDatas(DICT_TYPE.COLLEGE_LIST),
    };
  },
  created() {
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
  },
  methods: {


    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime];
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:show", false);
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        departmentNumber: undefined,
        departmentName: undefined,
        code: undefined,
        collegeId: undefined,
        userId: undefined,
        openingTime: undefined,
        orgId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateDepartments(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit("success");
          });
          return;
        }
        // 添加的提交
        createDepartments(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },
  },
};
</script>
